define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree', 'cookie'], function ($, undefined, Backend, Table, Form, undefined, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dd/user/index' + location.search,
                    add_url: 'dd/user/add2',
                    edit_url: 'dd/user/edit',
                    del_url: 'dd/user/del',
                    multi_url: 'dd/user/multi',
                    import_url: 'dd/user/import',
                    table: 'dd_user',
                }
            });

            var table = $("#table");

            var buttons = [];
            buttons.push({
                name: 'op1',
                title: __('分配额度'),
                text: __('分配额度'),
                classname: 'btn btn-xs btn-info btn-dialog',
                url: 'dd/user/edfp/'
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'login_name', title: __('Login_name'), operate: 'LIKE'},
                        //{field: 'login_pass', title: __('Login_pass'), operate: 'LIKE'},
                        //{field: 'openid', title: __('Openid'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        //{field: 'openidb', title: __('Openidb'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        //{field: 'openida', title: __('Openida'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        //{field: 'headimg', title: __('Headimg'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        //{field: 'sub_time', title: __('Sub_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'subscribe', title: __('Subscribe')},
                        //{field: 'parent1', title: __('Parent1')},
                        //{field: 'parent2', title: __('Parent2')},
                        //{field: 'parent3', title: __('Parent3')},
                        //{field: 'agent1', title: __('Agent1')},
                        //{field: 'agent2', title: __('Agent2')},
                        //{field: 'agent3', title: __('Agent3')},
                        {field: 'money', title: __('Money'), operate:false},
                        //{field: 'num', title: __('Num')},
                        //{field: 'yingkui', title: __('Yingkui'), operate:'BETWEEN'},
                        {field: 'points', title: __('Points'), operate:false},
                        {field: 'expense', title: __('Expense'), operate:false},
                        //{field: 'withdraw', title: __('Withdraw'), operate:false},
                        //{field: 'parent4', title: __('Parent4')},
                        //{field: 'parent5', title: __('Parent5')},
                        //{field: 'parent6', title: __('Parent6')},
                        //{field: 'parent7', title: __('Parent7')},
                        //{field: 'parent8', title: __('Parent8')},
                        //{field: 'parent20', title: __('Parent20')},
                        //{field: 'parent19', title: __('Parent19')},
                        //{field: 'parent18', title: __('Parent18')},
                        //{field: 'parent17', title: __('Parent17')},
                        //{field: 'parent16', title: __('Parent16')},
                        //{field: 'parent15', title: __('Parent15')},
                        //{field: 'parent14', title: __('Parent14')},
                        //{field: 'parent13', title: __('Parent13')},
                        //{field: 'parent12', title: __('Parent12')},
                        //{field: 'parent11', title: __('Parent11')},
                        //{field: 'parent10', title: __('Parent10')},
                        //{field: 'parent9', title: __('Parent9')},
                        //{field: 'is_tong', title: __('Is_tong')},
                        {field: 'type', title: __('Type')},
                        //{field: 'yong', title: __('Yong'), operate:'BETWEEN'},
                        //{field: 'yongtime', title: __('Yongtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'zhifubao', title: __('Zhifubao'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        //{field: 'xingming', title: __('Xingming'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        //{field: 'cashimg', title: __('Cashimg'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        //{field: 'lahei', title: __('Lahei')},
                        //{field: 'zbbz', title: __('Zbbz'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        //{field: 'ip', title: __('Ip'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        //{field: 'openidtx', title: __('Openidtx'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'handicap', title: __('Handicap'), operate: false},
                        //{field: 'agent_id', title: __('Parent_id')},
                        {field: 'game1_refund', title: __('Game1_refund'), operate: false},
                        {field: 'game2_refund', title: __('Game2_refund'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons: buttons, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        add2: function () {
            new Vue({
                el: '#configIndex',
                data() {
                    return {
                        active: 0,
                        pagentname:"swe",
                        stitle: "daf1",
                        sdatetime: "323sd",
                        szvalue: false,
                        wjzh: "",
                        bzmc: "vad22323",
                        mima: "123456",
                        mima2: "123456",
                        czmima: "123423",
                        bzinfo: "",
                        game1_refund: 0.8,
                        game2_refund: 0,
                        game1_limit: 10000,
                        game2_limit: 10000,
                        handicap:"",
                        size: '',
                        nexttitle: "下一步",
                        tableData: [],
                        multipleSelection: [],
                        newdduser_id:0
                    };
                },
                mounted: function () {
                    this.pagentname = WSdata.agentself.name;
                    console.log(WSdata.agentself);
                    this.getinfo();
                },
                methods: {
                    getinfo() {
                        //this.tableData
                        let that = this;
                        var url = "/api/bjl/gethandicaplist";
                        Fast.api.ajax({
                            url: url,
                            loading: 0
                        }, function (data, ret) {
                            console.log(data);
                            that.tableData = data;

                            return false;
                        }, function (data, ret) {
                            return false;
                        });
                    },
                    next() {
                        if (this.active == 0) {
                            if (this.wjzh.length < 1 || this.bzmc.length < 2 || this.mima.length < 2 || this.mima != this.mima2) {
                                this.$message({
                                    type: 'error',
                                    message: '账号密码输入错误!'
                                });
                                return;
                            }
                        }

                        this.active++;
                        if (this.active == 4) {
                            this.nexttitle = "提交";
                        }

                        if (this.active == 5) {
                            console.log("3221");

                            var url = "/api/bjl/postuser";
                            let that = this;
                            Fast.api.ajax({
                                url: url,
                                data: {
                                    pid: WSdata.agentself.id,
                                    wjzh: that.wjzh,
                                    bzmc: that.bzmc,
                                    mima: that.mima,
                                    bzinfo: that.bzinfo,
                                    game1_limit: that.game1_limit,
                                    game2_limit: that.game2_limit,
                                    game1_refund: that.game1_refund,
                                    game2_refund: that.game2_refund,
                                    czmima: that.czmima,
                                    handicap: that.handicap
                                },
                            }, function (data, ret) {
                                console.log(data);
                                that.newdduser_id = data.id;
                                that.todo();
                                return true;
                            }, function () {
                                return true;
                            });
                        }
                        //console.log(this.multipleSelection);
                    },
                    todo() {
                        this.$confirm('创建成功，请选择继续分配额度或返回列表?', '提示', {
                            confirmButtonText: '返回列表',
                            cancelButtonText: '继续分配额度',
                            center: true,
                            type: 'warning'
                        }).then(() => {
                            this.$message({
                                type: 'success',
                                message: '返回列表成功!'
                            });

                            var index = parent.Layer.getFrameIndex(window.name);
                            parent.Layer.close(index);

                        }).catch(() => {
                            this.$message({
                                type: 'info',
                                message: '继续分配额度'
                            });

                            let index = parent.Layer.getFrameIndex(window.name);

                            var url = 'dd/user/edfp/ids/' + this.newdduser_id;
                            var options = {
                                shadeClose: false,
                                shade: [0.3, '#393D49'],
                                area: ["80%", "80%"],
                                callback: function (data) {
                                    console.log(data);
                                    parent.Layer.close(index);
                                }
                            };
                            Fast.api.open(url, "继续分配额度", options);

                        });
                    },
                    back() {
                        this.active--;
                        if (this.active < 1) {
                            this.active = 0;
                        }
                        this.nexttitle = "下一步";
                    },
                    toggleSelection(rows) {
                        if (rows) {
                            rows.forEach(row => {
                                this.$refs.multipleTable.toggleRowSelection(row);
                            });
                        } else {
                            this.$refs.multipleTable.clearSelection();
                        }
                    },
                    handleSelectionChange(val) {
                        this.multipleSelection = val;
                        this.handicap = "";
                        for (let item of val) {
                            console.log(item);
                            this.handicap += item.id + ",";
                        }
                        if (this.handicap.length > 1) {
                            this.handicap = this.handicap.substr(0, this.handicap.length-1);
                        }
                        console.log("handicap=" + this.handicap);
                    },
                    handleChange(newVal) {
                        console.log('Switch value changed to:', newVal);
                        // 在这里，你可以根据 newVal（新的开关状态）执行一些逻辑
                        if (newVal) {
                            this.wjzh = this.generateComplexRandomAccount(10);
                        } else {
                            this.wjzh = "";
                        }
                    },
                    generateComplexRandomAccount(length) {
                        var result = '';
                        var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
                        var requiredTypes = {
                            uppercase: false,
                            lowercase: false,
                            digit: false
                        };
                        var charactersLength = characters.length;

                        while (Object.values(requiredTypes).includes(false) && result.length < length) {
                            var randomIndex = Math.floor(Math.random() * charactersLength);
                            var randomChar = characters.charAt(randomIndex);

                            if (
                                (randomChar >= 'A' && randomChar <= 'Z' && !requiredTypes.uppercase) ||
                                (randomChar >= 'a' && randomChar <= 'z' && !requiredTypes.lowercase) ||
                                (randomChar >= '0' && randomChar <= '9' && !requiredTypes.digit)
                            ) {
                                result += randomChar;
                                if (randomChar >= 'A' && randomChar <= 'Z') requiredTypes.uppercase = true;
                                if (randomChar >= 'a' && randomChar <= 'z') requiredTypes.lowercase = true;
                                if (randomChar >= '0' && randomChar <= '9') requiredTypes.digit = true;
                            }
                        }

                        // If the account is still too short, fill it with random characters
                        while (result.length < length) {
                            result += characters.charAt(Math.floor(Math.random() * charactersLength));
                        }

                        return result;
                    }
                }

            })

        },
        edfp: function () {
            new Vue({
                el: '#configIndex',
                data() {
                    return {
                        cred: 0,
                        mima: "",//操作密码
                        pagentname:"",
                        wjzh: "",
                        pyxed:"",
                        yxed:"",
                    };
                },
                mounted: function () {
                    this.pagentname = WSdata.agentself.name;
                    this.wjzh = WSdata.agentself.user.login_name;
                    this.pyxed = WSdata.agentself.user.game1_limit;
                    this.yxed =  WSdata.dduser.game1_limit;

                },
                methods: {
                    back() {
                        var index = parent.Layer.getFrameIndex(window.name);
                        parent.Layer.close(index);
                    },
                    next() {
                        if (this.mima != WSdata.dduser.czmima) {
                            this.$message({
                                type: 'error',
                                message: '操作密码错误!'
                            });
                            return;
                        }
                        var url = "/api/bjl/postedfp";
                        let that = this;
                        Fast.api.ajax({
                            url: url,
                            data: {
                                id: WSdata.dduser.id,
                                cred: that.cred
                            },
                        }, function (data, ret) {
                            that.$message({
                                type: 'succsee',
                                message: '分配成功!'
                            });
                            Fast.api.close(data);
                            return true;
                        }, function () {
                            return true;
                        });
                    },
                }

            })

        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
